package com.coderman.controller.business;

import com.coderman.business.service.IStudentService;
import com.coderman.common.annotation.ControllerEndpoint;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.consult.Consultant;
import com.coderman.common.model.student.Student;
import com.coderman.common.response.ResponseBean;
import com.coderman.common.vo.student.StudentVO;
import com.coderman.common.vo.system.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "学生管理")
@RestController
@RequestMapping("/student")
/**
 * 咨询管理控制层
 * @auther 文晓平
 * @version 1.0
 * @date 2023-1-9
 */
public class StudentController {

    @Autowired
    private IStudentService studentService;

    /**
     * 学员列表
     *
     * @return
     */
    @ApiOperation(value = "学员列表", notes = "模糊查询学员列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<Student>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                  StudentVO studentVO) {
        PageVO<Student> studenttList = studentService.findList(pageNum, pageSize, studentVO);
        return ResponseBean.success(studenttList);
    }


    /**
     * 添加学生信息
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加学生失败", operation = "添加学生")
    @RequiresPermissions({"student:add"})
    @ApiOperation(value = "添加学生")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean add(@RequestBody @Validated StudentVO studentVO) {
        studentService.add(studentVO);
        return ResponseBean.success();
    }

    /**
     * 编辑学生信息
     *
     * @param id 编号
     * @return
     */
    @ApiOperation(value = "编辑学生")
    @RequiresPermissions({"student:edit"})
    @GetMapping("/edit/{id}")
    @ResponseBody
    public ResponseBean edit(@PathVariable Integer id) throws SystemException {
        StudentVO studentVO = studentService.edit(id);
        return ResponseBean.success(studentVO);
    }

    /**
     * 更新学生操作
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新学生失败", operation = "更新学生")
    @ApiOperation(value = "更新学生")
    @RequiresPermissions({"student:update"})
    @PutMapping("/update/{id}")
    @ResponseBody
    public ResponseBean update(@PathVariable Integer id, @RequestBody @Validated StudentVO studentVO) throws SystemException {
        studentService.update(id, studentVO);
        return ResponseBean.success();
    }

    /**
     * 删除学生信息
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除学生信息失败", operation = "删除学生信息")
    @ApiOperation(value = "删除咨询信息")
    @RequiresPermissions({"student:delete"})
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResponseBean delete(@PathVariable Integer id) throws SystemException {
        studentService.delete(id);
        return ResponseBean.success();
    }


    /**
     * 更新状态
     *
     * @param id
     * @param status
     * @return
     */
//    @ControllerEndpoint(exceptionMessage = "更新咨询者状态失败", operation = "用户|已报名/未报名")
//    @ApiOperation(value = "状态", notes = "未报名和已报名这两种状态")
//    @RequiresPermissions({"consult:status"})
//    @PutMapping("/updateStatus/{id}/{status}")
//    @ResponseBody
//    public ResponseBean updateStatus(@PathVariable Integer id, @PathVariable Integer status) throws SystemException {
//        consultantService.updateStatus(id, status);
//        return ResponseBean.success();
//    }


    /**
     * 导出excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有学员的excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("consult:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出学生excel")
    public void export(HttpServletResponse response) {
        List<Student> students = this.studentService.findAll();
        ExcelKit.$Export(Consultant.class, response).downXlsx(students, false);
    }


}
